package com.woorigil.social.controller.member;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.woorigil.social.domain.member.Member;
import com.woorigil.social.domain.member.MemberDao;
import com.woorigil.social.domain.member.Subscribe;
import com.woorigil.social.domain.member.SubscribeDao;
import com.woorigil.social.validation.EmailValidation;

@Controller
public class MemberController {
	@Autowired MemberDao memberDao;
	@Autowired SubscribeDao subscribeDao;
	
	@RequestMapping("/admin/subscribe/list")
	public ModelAndView subscribeList(){
		List<Subscribe> subscribeList = subscribeDao.selectList();
		ModelAndView mav = new ModelAndView("admin/subscribe/list");
		mav.addObject("subscribeList", subscribeList);
		return mav;
	}
	
	@RequestMapping("/member/subscribe")
	public ModelAndView subscribe(String email, String keyword) {
		ModelAndView mav = new ModelAndView("");
		if (!EmailValidation.isValid(email)) {
			mav.addObject("status", "FAIL");
			mav.addObject("message", "잘못된 이메일 형식입니다.");
			return mav;
		}

		Subscribe subscribe = subscribeDao.select(email);
		if (subscribe == null) {
			subscribeDao.insert(new Subscribe(email, keyword));
		} else {
			subscribe.addKeyword(keyword);
			subscribeDao.update(subscribe);
		}
		
		mav.addObject("status", "SUCCESS");
		mav.addObject("message", "구독회원이 되신것을 축하드립니다.");
		return mav;
	}	
	
	@RequestMapping("/member/findKeyword")
	public ModelAndView findKeyword(String email, String keyword) {
		ModelAndView mav = new ModelAndView("");

		Subscribe subscribe = subscribeDao.select(email);
		if (subscribe == null) {
			mav.addObject("status", "FAIL");
			mav.addObject("message", "구독회원이 아닙니다. 구독하기버튼을 클릭하세요.");
			return mav;
		}
		
		if (StringUtils.isBlank(subscribe.getKeyword())) {
			mav.addObject("message", "등록된 관심상품이 없습니다. 관심상품을 입력해주세요.");
		}
		
		mav.addObject("status", "SUCCESS");
		mav.addObject("keyword", subscribe.getKeyword());
		return mav;
	}	
	
	@RequestMapping("/member/joinForm") //url (....html)
	public ModelAndView joinForm() {
		ModelAndView mav = new ModelAndView("member/joinForm"); //페이지 (jsp)
		return mav;
	}
	
	@RequestMapping("/member/join")
	public ModelAndView join(Member member) {
		//필수값 체크(id, 이름...)
		//System.out.printf("id = "+ member.getId());
		
		member.setRegisteDate(new Date());
		
		if (member.getId() == null) {
			new IllegalArgumentException("아이디값을 넣으세요.");
			joinForm();
		}else if(member.getName() == null){
			new IllegalArgumentException("이름값을 넣으세요.");
			joinForm();
		}else if(member.getPassword() == null){
			new IllegalArgumentException("비밀번호값을 넣으세요.");
			joinForm();
		}else if(member.getBirthday() == null){
			new IllegalArgumentException("생년월일값을 넣으세요.");
			joinForm();
		}else if(member.getEmailAgreeYn() == null){
			new IllegalArgumentException("이메일 수신여부를 선택하세요.");
			joinForm();
		}else if(member.getSex() == null){
			new IllegalArgumentException("성별을 선택하세요.");
			joinForm();
		}else if(member.getPersonalAgreeYn() == null){
			new IllegalArgumentException("개인정보동의여부를 선택하세요.");
			joinForm();
		}
		
		memberDao.insert(member);
		ModelAndView mav = new ModelAndView(new RedirectView("/member/joinSuccess.html"));
		return mav;
	}
	
	@RequestMapping("/member/joinSuccess")
	public ModelAndView success(){
		ModelAndView mav = new ModelAndView("member/joinSuccess");
		return mav;
	}
	
	@RequestMapping("/member/view")
	public ModelAndView view(){
		ModelAndView mav = new ModelAndView("member/view"); 
		return mav;
	}
	
	@RequestMapping("/admin/member/view")
	public ModelAndView adminView(int seq){
		
		Member member = memberDao.select(seq);
		ModelAndView mav = new ModelAndView("admin/member/view");
		mav.addObject("member", member);
		return mav;
	}
	
	@RequestMapping("/member/update")
	public ModelAndView update(Member member, HttpSession session){
		
		member.setModifyDate(new Date());
		
		if (member.getId() == null) {
			new IllegalArgumentException("아이디값을 넣으세요.");
			joinForm();
		}else if(member.getName() == null){
			new IllegalArgumentException("이름값을 넣으세요.");
			joinForm();
		}else if(member.getPassword() == null){
			new IllegalArgumentException("비밀번호값을 넣으세요.");
			joinForm();
		}else if(member.getBirthday() == null){
			new IllegalArgumentException("생년월일값을 넣으세요.");
			joinForm();
		}else if(member.getEmailAgreeYn() == null){
			new IllegalArgumentException("이메일 수신여부를 선택하세요.");
			joinForm();
		}else if(member.getSex() == null){
			new IllegalArgumentException("성별을 선택하세요.");
			joinForm();
		}else if(member.getPersonalAgreeYn() == null){
			new IllegalArgumentException("개인정보동의여부를 선택하세요.");
			joinForm();
		}
		
		memberDao.update(member);
		if(session != null){
			session.setAttribute("member", member);
		}
		
		ModelAndView mav = new ModelAndView("member/joinSuccess");
		return mav;
	}
	
	@RequestMapping("/admin/member/list")
	public ModelAndView list(){
		List<Member> memberList = memberDao.selectList();
		
		ModelAndView mav = new ModelAndView("admin/member/list");
		mav.addObject("memberList", memberList);
		return mav;
	}
	
	@RequestMapping("/admin/member/delete")
	public ModelAndView delete(int seq){
		
		memberDao.delete(seq);
		
		ModelAndView mav = new ModelAndView(new RedirectView("/admin/member/list.html"));
		return mav;
	}
}
